# 第二章深度学习通用流程之数据下载练习题
import os
import torch
import torch.hub as hub
import certifi
import locale
#def set_env_as_CN():
    #os.environ['PYTHONIOENCODING'] = 'utf-8' # 设置Python输入输出编码为UTF-8
    #os.environ['LC_ALL'] = 'zh_CN.UTF-8' # 设置中文环境，以便解决中文路径问题，作用在系统环境变量（底层）。
    #locale.setlocale(locale.LC_ALL, 'zh_CN.UTF-8') # 设置Python的区域设置为中文（作用在Python层面）
def SSL_CERT_FILE():
    os.environ['SSL_CERT_FILE'] = certifi.where()
    print(f"设置SSL_CERT_FILE : {os.environ['SSL_CERT_FILE']}")

def download_file(url,save_path):
    os.makedirs(save_path,exist_ok=True)# 创建目录
    file_name = os.path.basename(url)
    full_path = os.path.join(save_path,file_name)
    try:
        print("开始下载")
        hub.download_url_to_file(url,full_path,progress=True)
        if os.path.exists(full_path):
            print(f"下载完成，文件保存在: {full_path}")
        else:
            print("文件下载失败")
    except Exception as e:
        print(f"下载过程中出现错误: {e}")

def main():
    #set_env_as_CN()
    SSL_CERT_FILE()
    url = "https://arxiv.org/pdf/1512.03385.pdf"
    save_path = os.path.expanduser(".\\Desktop\\Pytorch与深度学习实战\\code\\第二章\\tmp")
    download_file(url,save_path)
if __name__ == "__main__":
    main()
